package proxy.dynamic.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author Feng
 * @date 2019/3/12 21:19
 */
public class JDKMeipo implements InvocationHandler {

    private Object object;

    public Object getInstance(Object target){
        this.object = target;
        Class<?> clazz = target.getClass();
        return Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object object = method.invoke(this.object, args);
        after();
        return object;
    }

    private void after() {
        System.out.println("如果合适的话就开始办事");
    }

    private void before() {
        System.out.println("我是媒婆，开始给你找对象，现在已经拿到你的需求");
        System.out.println("开始物色");
    }
}
